"""
配置分为两部分：
1. 通用配置(common)， 如token过期时间，不能发在源代码中, 存放在src/settings下，
2. 私密配置(secret)，如帐号、密码, 不能放在源代码中
"""
import os
import sys
from django.core.management.color import color_style
from simple_config import Config
from settings import default_conf

_SERVER_TYPE_LST = ['DEV', 'SIT', 'PRD']


class ProjectConfig(Config):

    def load_config(self):
        self.from_object(default_conf)   # 加载默认配置
        self.load_secret_config()        # 加载私密配置
        # self.load_common_config()        # 加载通用配置

    def load_common_config(self):
        assert self.SERVER_TYPE in _SERVER_TYPE_LST
        config_file = self.get_common_config_filename(
            self.COMMON_CONF_DIR, self.SERVER_TYPE)
        if self.SERVER_TYPE == 'DEV' and not os.path.exists(config_file):
            msg = '[SERVER_TYPE: %s] config_file: %s does not exist' % (
                self.SERVER_TYPE, config_file)
            config_file = self.get_common_config_filename(
                self.COMMON_CONF_DIR, 'SIT')
            msg = '%s, try to use %s as alternative\n' % (msg, config_file)
            style = color_style()
            sys.stdout.write(style.NOTICE(msg))
        self.from_ini(config_file)

    def load_secret_config(self):
        """加载私密配置
        """
        self.from_ini(self.SECRET_CONF_FILE)

    @staticmethod
    def get_common_config_filename(conf_dir, server_type):
        return os.path.join(conf_dir, 'env_%s.ini' % server_type.lower())


config = ProjectConfig()
config.load_config()

sys.modules[__name__] = config
